const { uniacid } = require("../../host");

// packageG/offlineHongbao/offlineHongbao.js
var o = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    moneyList: [],
    lingquList: [

    ],
    statusBarHeight: 0, // 状态栏高度
    navBarHeight: 0, // 导航栏高度
    navStatusBarHeight: 0, // 导航栏和状态栏高度
    menu: 0, // 胶囊
    tabslist: [{
        title: '红包列表'
      },
      {
        title: '领取记录'
      }
    ],
    tabsIndex: 0, //0消费金额 1领取记录
    allChecked: false, //全选
    hongbaoShow: false, //领红包弹窗
    amount: 0, //红包总额度
    is_draw: 0, //领取总金额
    itemid: [],
    newmoney: '',
    countPage: '',
    page: 1,
    result: [],
    isDisableClick: false,//单个领取红包防止重复点击
    isDisableClickAll: false,//多选领取红包防止重复点击
  },
  onChange(event) {
    // console.log(event.detail,'')
    this.setData({
      result: event.detail,
    });
  },
  changeTabs(e) {
    this.setData({
      tabsIndex: e.currentTarget.dataset.index
    })
    this.getData()
  },
  
  // 改变全选状态
  changeAllCheck() {
    this.setData({
      allChecked: !this.data.allChecked
    })
    if (this.data.allChecked == true) {
      let arr = []
      this.data.moneyList.forEach((item) => {
        arr.push(item.id + '')
      })
      console.log(this.data.result, arr, 'this.data.result')
      this.setData({
        result: arr
      })
    } else {
      this.setData({
        result: []
      })
    }
  },
  goback() {
    wx.navigateBack({
      delta: 1
    })
  },
  // 全选--领红包
  lingHongAll(){
    if(this.data.result.length <= 0){
      wx.showToast({
        title: '请选择要领取的红包',
        icon: 'none'
      })
      return;
    }
    this.setData({
      isDisableClickAll: true
    })
    
    var e = this,
      t = o.getNetAddresss("Yingguang.lingqu");
    o._getNetWork({
      url: t,
      data: {
        ids: JSON.stringify(e.data.result)
      },
      success: function (a) {
        if (a.data.result == 1) {
          wx.showToast({
            title: '领取成功',
            icon: 'none'
          })
          e.setData({
            isDisableClickAll: false
          })
          e.getData();
        } else {
          wx.showToast({
            title: '领取失败',
            icon: 'none'
          })
          e.setData({
            isDisableClickAll: false
          })
        }
      }
    });
  },
  // 单选--领红包
  lingHong(e) {
    this.setData({
      isDisableClick: true
    })
    let id = e.currentTarget.dataset.id
    var e = this,
      t = o.getNetAddresss("Yingguang.lingqu");
    o._getNetWork({
      url: t,
      data: {
        ids: JSON.stringify([id])
      },
      success: function (a) {
        if (a.data.result == 1) {
          wx.showToast({
            title: '领取成功',
            icon: 'none'
          })
          e.setData({
            isDisableClick: false
          })
          e.getData();
        } else {
          wx.showToast({
            title: '领取失败',
            icon: 'none'
          })
          e.setData({
            isDisableClick: false
          })
        }
      }
    });
  },
  //关闭领取红包弹窗
  closeHongPop() {
    this.setData({
      hongbaoShow: false
    })
    this.getXiaofeiMoney()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getSystemInfo()
    if(!wx.getStorageSync("yz_uid")){
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
    
    this.getData();
  },
  // 领取记录
  getRecordList() {
    var e = this,
      t = o.getNetAddresss("FeilongStore.drawList");
    o._getNetWork({
      url: t,
      data: {
        uid: wx.getStorageSync("yz_uid"),
        page: this.data.page
      },
      success: function (a) {
        console.log(a.data.data.data, '看看是啥')
        if (a.data.result == 1) {
          e.setData({
            countPage: a.data.data.last_page,
          })
          if (e.data.page == 1) {
            var lingquList = a.data.data.data
          } else {
            var lingquList = e.data.lingquList
            lingquList = lingquList.concat(a.data.data.data)
          }
          e.setData({
            lingquList: lingquList,
          })
          console.log('09090', lingquList)
        }
      }
    });
  },
  getData() {
    var e = this,
      t = o.getNetAddresss("Yingguang.hongbaolist");
    o._getNetWork({
      url: t,
      data: {
        uid: wx.getStorageSync("yz_uid"),
      },
      success: function (a) {
        if (a.data.result == 1) {
          e.setData({
            amount: a.data.data.all_price,
            is_draw: a.data.data.jin_price,
            moneyList: a.data.data.wlq_list,
            lingquList: a.data.data.ylq_list
          })
        }
      }
    });
  },
  getSystemInfo() {
    let _this = this;
    // 获取手机系统信息
    wx.getSystemInfo({
      success: res => {
        // 手机系统状态栏高度
        //   wx.setStorageSync('statusBarHeight', res.statusBarHeight)
        _this.setData({
          statusBarHeight: res.statusBarHeight
        })
        const platform = res.platform
        const menu = wx.getMenuButtonBoundingClientRect()
        //menu为胶囊，判断是否能读到胶囊位置，读不到则用具体一般数值表示
        if (menu) {
          _this.setData({
            menu: menu,
            navBarHeight: menu.height + (menu.top - res.statusBarHeight) * 2,
            navStatusBarHeight: res.statusBarHeight + menu.height + (menu.top - res.statusBarHeight) * 2
          })
          // wx.setStorageSync('menu', menu)
          _this.menu = menu

          // 导航栏高度
          // wx.setStorageSync('navBarHeight', menu.height+(menu.top-res.statusBarHeight) *2 )
          _this.navBarHeight = menu.height + (menu.top - res.statusBarHeight) * 2
          // 状态栏加导航栏

          // wx.setStorageSync('navStatusBarHeight', res.statusBarHeight+ menu.height+(menu.top-res.statusBarHeight) *2 )
          _this.navStatusBarHeight = res.statusBarHeight + menu.height + (menu.top - res.statusBarHeight) * 2

        } else {
          _this.setData({
            menu: null,
            navBarHeight: platform === 'android' ? 48 : 44,
            navStatusBarHeight: res.statusBarHeight + (platform === 'android' ? 48 : 44)
          })
          // wx.setStorageSync('menu', null)
          _this.menu = null
          // 导航栏高度
          // wx.setStorageSync('navBarHeight', platform === 'android' ? 48 : 44)
          _this.navBarHeight = platform === 'android' ? 48 : 44
          // 状态栏加导航栏

          //  wx.setStorageSync('navStatusBarHeight', res.statusBarHeight+ (platform === 'android' ? 48 : 44) )
          _this.navStatusBarHeight = res.statusBarHeight + (platform === 'android' ? 48 : 44)
        }
      },
      fail(err) {
        console.log(err);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(!wx.getStorageSync("yz_uid")){
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // this.data.page = 1;
    // this.getRecordList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log('到达底部')
    // if (this.data.countPage > this.data.page) {
    //   this.data.page++;
    //   this.getRecordList()
    // } else {
    //   if (this.data.page != 1) {
    //     wx.showToast({
    //       title: '已加载全部',
    //       icon: 'none'
    //     })
    //   }
    // }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})